package com.beone.admin.controller;

import com.beone.admin.controller.common.ControllerUtils;
import com.beone.admin.form.TaskForm;
import com.beone.admin.query.TaskQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.beone.admin.exception.MvcException;
import com.beone.admin.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.beone.admin.common.AdminConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.beone.admin.entity.Task;
import com.beone.admin.service.TaskService;
import java.io.Serializable;

import org.springframework.stereotype.Controller;
import com.base.SuperController;

import javax.servlet.http.HttpServletRequest;


/**
 * @Title 任务反馈 前端控制器
 * @Author 秦波
 * @Version 1.0 on 2019-05-18
 * @Copyright 贝旺科权
 */
@Controller
@RequestMapping("/admin/task")
@Api(value = "任务反馈管理模块", tags = {"任务反馈管理接口"})
public class TaskController extends SuperController {

private static final Logger log = LoggerFactory.getLogger(TaskController.class);

@Autowired
private TaskService taskService;

    /**
     * 任务反馈管理 入口 --> index
     * @return
     */
    @GetMapping("")
    public String index() throws MvcException{
        return "task/index";
    }

    /**
     * 任务反馈管理 显示添加页面
     * @return
     */
    @GetMapping("/showAdd")
    public String showAdd()  throws MvcException{
        return "task/form";
    }

    /**
     * 任务反馈管理 显示附件页面
     * @return
     */
    @GetMapping("/showAttachment/{id}")
    public String showAttachment(@PathVariable("id") String id, ModelMap model)  throws MvcException{
        model.addAttribute("id", id);
        return "task/attachment";
    }

    /**
     * 任务反馈管理 显示修改页面
     * @return
     */
    @GetMapping("/showUpdate/{id}")
    public String showUpdate(@PathVariable("id") String id, ModelMap model) throws MvcException{
        try {
            model.addAttribute("id", id);
            model.addAttribute("entity", taskService.selectById(id));
        }catch (Exception e) {
            log.error("showUpdate 异常 e = {}", e);
            throw new MvcException(e.getMessage());
        }
        return "task/form";
    }

    /**
     * 任务反馈管理分页列表 JSON
     * @param query
     * @return
     */
    @ApiOperation(value = "任务反馈列表", notes = "任务反馈列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true
                    , paramType = "param", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页最大显示记录数", required = true
                    , paramType = "param", dataType = "int")
    })
    @RequestMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE
            , method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object showList(HttpServletRequest request, TaskQuery query) {
        query.setUserId(ControllerUtils.getAdminUser(request).getUserId());
        return  taskService.getTaskPagination(query);
    }

    /**
     *  保存任务反馈
     * @param form
     * @return
     */
    @ApiOperation(value = "保存任务反馈", notes = "保存任务反馈 主键属性为空新增,否则修改")
    @ApiImplicitParam(name = "entity", value = "{title}详细实体", required = true
            , paramType = "form", dataType = "object")
    @Log(desc = "任务反馈", type = Log.LOG_TYPE.SAVE)
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Object save(HttpServletRequest request, TaskForm form) {
        form.setUserId(ControllerUtils.getAdminUser(request).getUserId());
        if(taskService.insertOrUpdate(form)){
            return super.responseResult("success", "成功！");
        }
        return super.responseResult("fail", "失败！");
    }

;
    /**
     * 删除任务反馈
     * @param id
     * @return
     */
    @ApiOperation(value = "删除任务反馈", notes = "根据任务反馈Id，删除任务反馈")
    @ApiImplicitParam(name = "id", value = "实体ID", required = true, paramType = "param", dataType = "object")
    @Log(desc = "删除任务反馈", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id) {
        if(taskService.deleteById(id)){
            return super.responseResult("success", "成功！");
        }
        return super.responseResult("fail", "失败！");
    }
}
